#!/bin/bash

 grep "binding" result.output|awk '{print $1}'>bound.dat
 grep "binding" result.output|awk '{print 100*$5-1}'>frame.dat

j=1
while read i
do
  row=`awk -v line=$j 'NR==line {print $1}' frame.dat`
  echo " $i traj ## :  $row frame ##">>PreBoundState.dat
  awk -v line=$row 'NR==line {print}' k$i.dat>> PreBoundState.dat
  j=`expr $j + 1`
done< bound.dat

 awk '!/#$/ {m=n=0;for(l=4;l<=6;l+=1)if($l>m){m=$l;n=l}; print n,$n}' PreBoundState.dat> PreSuccess.dat
NOne=`grep "^4 " PreSuccess.dat |wc -l`
NSecond=`grep "^5 " PreSuccess.dat |wc -l`
Ns=`grep "^6 " PreSuccess.dat |wc -l`
Ntotal=`cat PreSuccess.dat |wc -l`
POne=$(echo "scale=4;$NOne/$Ntotal"|bc)
PSecond=`echo "scale=4;${NSecond}/${Ntotal}" | bc`
Ps=`echo "scale=4;${Ns}/${Ntotal}" | bc`
 echo "preBound region probability: hO(REGION1)   hS(REGION2)   sheet(REGION3)"
 echo "$POne" "$PSecond" "$Ps"

rm bound.dat
rm frame.dat
rm PreBoundState.dat
rm PreSuccess.dat
